#!/usr/bin/env python

# app.py

r'''Some goofy Tkinter toy.
'''

from functools import partial
import cPickle as pickle
import os

from Tkinter import *
import tkFont
import tkFileDialog
import ttk

from ot_simulator.tk_gui.toplevel import toplevel
from ot_simulator.tk_gui.test.foo import foo
from ot_simulator.tk_gui.test.bar import bar

classes = {"foo": foo, "bar": bar}

button_text_font = 16 #tkFont.Font(size=16)
button_width = 10

class App(object):
    def __init__(self, master):
        self.frame = ttk.Frame(master)
        self.frame.pack(fill=BOTH, expand=1)

        Button(self.frame, text="Open", default=ACTIVE,
                   font=button_text_font,
                   width=button_width,
                   command=self.open) \
          .grid(row=0, column=0)
        Button(self.frame, text="New", font=button_text_font,
               width=button_width,
               command=self.new) \
          .grid(row=0, column=1)
        Button(self.frame, text="Quit", font=button_text_font,
               width=button_width,
               command=self.frame.quit) \
          .grid(row=0, column=2)

    def setup_filename(self, row, browse_command):
        ans = ttk.Entry(self.frame)
        ans.grid(row=row, column=1, sticky=W+E)
        ttk.Button(self.frame, text="Browse...",
               command=partial(self.set_entry, ans, browse_command)) \
          .grid(row=row, column=2)
        return ans

    def browse_input(self):
        # options:
        #   defaultextension (ignored by open dialog)
        #   filetypes sequence of (label, pattern)
        #   initialdir
        #   initialfile
        #   parent
        #   title
        return tkFileDialog.askopenfilename(title="Open File",
                                            parent=self.frame,
                                            filetypes=(
                                                ("bar file", ".bar"),
                                                ("foo file", ".foo"),
                                              ))

    def browse_output(self):
        return tkFileDialog.asksaveasfilename(title="Output File",
                                              parent=self.frame,
                                              filetypes=(
                                                  ("bar file", ".bar"),
                                                  ("foo file", ".foo"),
                                                ))

    def set_entry(self, entry, browse_command):
        name = browse_command()
        #print "set_entry", name
        if name:
            entry.delete(0, END)
            entry.insert(END, name)
            entry.select_range(0, END)

    def get_operation(self):
        selections = self.operation.curselection()
        return tuple(self.operations[int(i)] for i in selections)

    def open(self):
        filename = self.browse_input()
        if filename:
            with open(filename, "r") as f:
                obj = pickle.load(f)
            toplevel.obj_editor(obj, self.frame)

    def new(self):
        filename = self.browse_output()
        if filename:
            basename = os.path.basename(filename)
            name, ext = basename.rsplit('.', 1)
            obj = classes[ext](name)
            toplevel.obj_editor(obj, self.frame)

def run():
    root = Tk()
    root.option_add('*tearOff', FALSE)  # for menus
    root.title('Ornamental Lathe Simulator')
    root.resizable(width=False, height=False)
    app = App(root)
    root.mainloop()
    root.destroy()

if __name__ == "__main__":
    run()
